/*********************/
/** ControlLightSettingActivity.java */
/*********************/
package atag.apps.mainApp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import atag.apps.mainApp.client.QueryPortPortPort;
import atag.apps.mainApp.client.QueryPortService;
import atag.runtime.DataItem;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;

public class ControlLightSettingActivity extends Activity {

	private NodeInfo m_myState;
	private DataPool m_dataPool;
	
	QueryPortService service = new QueryPortService();
    QueryPortPortPort port = service.getQueryPortPort();

	private String username, ID, partition;

	final Handler h_ControlLightSettingActivityHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			TextView lightReadingTV = (TextView) findViewById(R.id.lightReadingTV);
			lightReadingTV.setText((String) msg.obj);
		}
	};

	/** Handles event: Button changeB clicked */
	private final OnClickListener changeBClicked = new OnClickListener() {

		public void onClick(View v) {
			EditText lightSettingET = (EditText) findViewById(R.id.lightSettingET);
			String lightSettingString = lightSettingET.getText().toString();
			
			String roomNoString = partition.split(" ")[1];

			try {

				int lightSetting = Integer.parseInt(lightSettingString);
				int roomNo = Integer.parseInt(roomNoString);
				ControllerLightSetting m_ControllerLightSetting = new ControllerLightSetting();

				m_ControllerLightSetting.setID(m_myState.getMyId());
				m_ControllerLightSetting.setLightSetting(lightSetting);
				port.updateLightQuery(ID, lightSetting);
				m_dataPool.putData(new DataItem(
						IDConstants.D_CONTROLLERLIGHTSETTING,
						IDConstants.T_CONTROLLIGHTSETTING,
						m_ControllerLightSetting), "Room", roomNo);
				System.out
						.println("[ControlLightSetting] LightSetting updated to "
								+ lightSetting);
				Context context = getApplicationContext();
				CharSequence text = "[ControlLightSetting] LightSetting updated to "
						+ lightSetting;
				int duration = Toast.LENGTH_LONG;				
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				TextView lightSettingTV= (TextView) findViewById(R.id.lightSettingTV);
				int light=port.getLightQuery(ID);				
				lightSettingTV.setText(light+"");
			} catch (Exception e) {
				Context context = getApplicationContext();
				CharSequence text = "Exception:" + e;
				int duration = Toast.LENGTH_LONG;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				Log.e("Exception", e + "", e);
			}
		}
	};
	/** Handles event: Button viewB clicked */

	private final OnClickListener viewBClicked = new OnClickListener() {

		public void onClick(View v) {
			LightInfoRequest m_LightInfoRequest = new LightInfoRequest();

			m_LightInfoRequest.setID(m_myState.getMyId());
			m_LightInfoRequest.setPartitionLabel("Remote");
			m_LightInfoRequest.setPartitionNo(0);
			String roomNoString = partition.split(" ")[1];
			int roomNo = Integer.parseInt(roomNoString);
			DataItem m_dataitem = new DataItem(IDConstants.D_LIGHTINFOREQUEST,
					IDConstants.T_CONTROLLIGHTSETTING, m_LightInfoRequest);
			m_dataPool.putData(m_dataitem, "Room", roomNo);

		}

	};

	/**
	 * Called when the activity is resumed from pause and when the activity is
	 * started
	 * 
	 */
	@Override
	public void onResume() {
		super.onResume();
		TextView lightSettingTV= (TextView) findViewById(R.id.lightSettingTV);
		try {
			int light=port.getLightQuery(ID);
			
			lightSettingTV.setText(light+"");
			
		} catch (Exception e) {
			Context context = getApplicationContext();
			CharSequence text = "Exception:" + e;
			int duration = Toast.LENGTH_LONG;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
			Log.e("Exception", e + "");
		}
	}
	
	/*
	 * Creates options menu.
	 * 
	 * @param menu the Menu to be populated
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	/*
	 * Handles menu item selections
	 * 
	 * @param item the selected MenuItem
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			return true;
		case R.id.exit:
			moveTaskToBack(true);
			return true;
		}
		return false;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Please add a layout in the res directory and also
		// add this activity in the manifest
		setContentView(R.layout.control_light_setting);
		SharedAndroidActivityHandlers.h_ControlLightSettingActivityHandler = h_ControlLightSettingActivityHandler;
		this.m_dataPool = SharedStoreForGUITasks.m_dataPool;
		this.m_myState = SharedStoreForGUITasks.m_myState;
		Intent i = getIntent();
		username = i.getStringExtra("username");
		ID = i.getStringExtra("ID");
		partition = i.getStringExtra("partition");
		if (username != null) {
			TextView welcomeTV = (TextView) findViewById(R.id.welcomeTV);
			welcomeTV.setText("Welcome " + username);
		}
		if (partition != null) {
			TextView partitionTV = (TextView) findViewById(R.id.partitionTV);
			partitionTV.setText(partition);
		}
		Button changeB = (Button) findViewById(R.id.changeB);
		Button viewB = (Button) findViewById(R.id.viewB);
		changeB.setOnClickListener(changeBClicked);
		viewB.setOnClickListener(viewBClicked);

	}
}
